fix: add boundary checks for u64 shift/rotate operations - #3368
Conversation
|
Automated check (CONTRIBUTING.md) Findings:
Next steps:
|
RationaleThe
This fix adds Test PlanAll tests in cargo test -p miden-core-lib shr_out_of_range_errors
cargo test -p miden-core-lib shl_out_of_range_errors
cargo test -p miden-core-lib rotl_out_of_range_errors
cargo test -p miden-core-lib rotr_out_of_range_errors |
|
/quality-review |
huitseeker
left a comment
There was a problem hiding this comment.
Thanks! Looks good overall. Please update the cycle counts and fix CI.
| #! [n, a_lo, a_hi, ...] -> [c_lo, c_hi, ...], where c = (a << n) mod 2^64. | ||
| #! This takes 21 cycles. | ||
| pub proc shl(n: u32, a: u64) -> u64 | ||
| dup u32lt.64 assert.err="shift amount must be in the range [0, 64)" |
There was a problem hiding this comment.
This adds work before the old body, so the cycle-count docs above this and the other three changed procs now look stale. Could you recompute and update the This takes ... cycles comments for shl, shr, rotl, and rotr?
One way to check them is to add a temporary core-lib test that wraps each exec.u64::* call with clk before and after the call, then computes the delta. Since clk itself costs one cycle, subtract the first clk instruction from the measured difference.
77c6e02 to
c144716
Compare
|
@huitseeker Review comments addressed:
PTAL! |
huitseeker
left a comment
There was a problem hiding this comment.
This will need a rebase on the latest origin/next, beware that the Changelog format has changed a bit
| #! Stack transition looks as follows: | ||
| #! [n, a_lo, a_hi, ...] -> [c_lo, c_hi, ...], where c = (a << n) mod 2^64. | ||
| #! This takes 21 cycles. | ||
| #! This takes 25 cycles. |
There was a problem hiding this comment.
I think these cycle counts are still low. The added check costs more than 4 cycles: u32lt.64 with an immediate is 4 cycles by itself, and the surrounding dup plus assert.err add 2 more.
Could you recompute these with a temporary clk-wrapped core-lib test before updating the comments? That should catch shl, shr, rotl, and rotr without having to count the expanded instructions by hand.
c144716 to
30464ea
Compare
|
@huitseeker Rebased on latest |
5770c06 to
82de925
Compare
|
You're right I recounted: |
82de925 to
f0c7580
Compare
|
Updated generated core library docs ( |
cca1b5f to
d6e075c
Compare
d6e075c to
6ebe174
Compare
|
@huitseeker Rebased on latest next and resolved CHANGELOG conflicts. All cycle counts and docs are updated. PTAL! |
6ebe174 to
6abe8aa
Compare
|
Rebased on latest next branch is up to date, all checks were passing on the previous push. @huitseeker ready for re-review when you get a chance. |
|
@huitseeker I've added a The test uses
The boundary check ( If any future change alters the cycle count, this test will catch it and report the exact mismatch. |
|
Have you had a chance to examine this, Sir? I made the necessary changes as requested. @huitseeker |
Signed-off-by: Sertug17 <104278804+Sertug17@users.noreply.github.com>
Replace individual measure_* tests with a single shift_rotate_cycle_baselines test following the project's established pattern (hash_precompile_cycle_baselines). Uses clk + mem_store/mem_load to measure cycle deltas and asserts against known baselines to catch unintended cycle count changes.
dc08da9 to
a93f5df
Compare
|
@huitseeker Rebased on latest next. All checks were passing before ready for re-review when you get a chance. |
|
Okay. I'll do it as soon as possible. @Al-Kindi-0 |
|
@Al-Kindi-0 Done removed the duplicate #3371 entry and moved the #3368 entry from v0.29.0 to the v0.30.0 (Unreleased) Fixes section. @huitseeker |
|
Thank you @Al-Kindi-0 . Did you have a chance to check @huitseeker ? |
|
This project has been on hold for a very long time. Would you be able to review it again? @huitseeker |
Fixes #3360
Add
dup u32lt.64 assert.errboundary checks to:u64::shlu64::shru64::rotlu64::rotrThese procedures documented that shift/rotation amounts must be in [0, 64), but lacked runtime enforcement. This matches the existing pattern in
u128::shr.Includes tests for
n = 64andn = 100for each procedure, as requested by @huitseeker.